---计划执行者，提供执行计划接口
---@class PlanHandler
---@field plan Queue
---@field actionManager ActionManagerBase
---@field onCompleteCallFunc function
---@field currentActionHandler ActionHandlerBase
---@field isInterruptible boolean
PlanHandler = class()

function PlanHandler:ctor()

end

function PlanHandler:IsComplete()
    if self.isInterruptible then
        return true;
    end
    if self.plan == nil then
        return true;
    end
    if self.currentActionHandler == nil then
        return self.plan:Size() == 0;
    else
        return self.currentActionHandler.excuteState == EActionExcuteState.EXIT and self.plan:Size() == 0;
    end
end

---@param actionManager ActionManagerBase
---@param plan Queue
function PlanHandler:Init(actionManager,plan)
    self.isInterruptible = false;
    self.currentActionHandler = nil;
    self.actionManager = actionManager;
    self.plan = plan;
end

---设置计划完成回调
function PlanHandler:SetCompleteCallBack(callFunc)
    self.onCompleteCallFunc = callFunc;
end

---开始计划
function PlanHandler:StartPlan()
    Debug.Log("开始执行计划")
    self:NextAction();
end

---执行下一个动作
function PlanHandler:NextAction()
    if self:IsComplete() then
        self.onCompleteCallFunc();
    else
        Debug.Log("计划剩余长度："..self.plan:Size());
        if self.plan:Size() == 0 then
            Debug.LogError("错误！计划剩余长度出现0，但计划并未终止！");
        end

        self.currentActionHandler = self.plan:Dequeue();
        Debug.Log("当前计划执行动作："..self.currentActionHandler.label);
        self.actionManager:ExcuteNewState(self.currentActionHandler.label);
    end
end

---设置被打断
function PlanHandler:Interruptible()
    self.isInterruptible = true;
end

---获取当前执行的动作
---@return ActionHandlerBase
function PlanHandler:GetCurrentHandler()
    return self.currentActionHandler;
end